<template>
  <div class="common-layout">
    <el-container>
      <el-header class="header">
        <div>
          <span style="color: #666">留言内容:</span>
          <el-input
            v-model="searchContent"
            style="width: 240px; margin: 0 12px"
            placeholder="请输入留言内容"
          />
          <el-button type="primary" style="margin: 0 12px" @click="handleSearch"
            >搜索</el-button
          >
        </div>
      </el-header>
      <el-main class="main">
        <el-divider />
        <el-table 
          :data="commentsList" 
          stripe 
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column prop="id" label="留言ID" width="80" />
          <el-table-column prop="content" label="留言内容" min-width="200" />
          <el-table-column prop="userNickname" label="用户昵称" width="120" />
          <el-table-column prop="productId" label="商品ID" width="100" />
          <el-table-column prop="createTime" label="创建时间" width="180" />
          <el-table-column label="操作" width="150">
            <template #default="scope">
              <el-button 
                type="danger" 
                size="small" 
                @click="handleDelete(scope.row.id)"
                :loading="deleteLoading[scope.row.id]"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer class="footer">
        <div class="demo-pagination-block">
          <el-pagination
            v-model:current-page="pagination.current"
            :page-size="pagination.size"
            :size="size"
            layout="total, prev, pager, next"
            :total="pagination.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-footer>
    </el-container>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import instance from '@/utils/request'

// 搜索内容
const searchContent = ref('')

// 数据状态
const commentsList = ref([])
const loading = ref(false)
const deleteLoading = ref({})

// 分页参数
const pagination = ref({
  current: 1,
  size: 8,
  total: 0
})

const size = ref('large')

// 搜索功能
const handleSearch = () => {
  pagination.value.current = 1
  fetchCommentsList()
  ElMessage.success('搜索成功！')
}

// 获取留言列表
const fetchCommentsList = async () => {
  loading.value = true
  try {
    const response = await instance.get('/review/list', {
      params: {
        page: pagination.value.current,
        pageSize: pagination.value.size,
        content: searchContent.value
      }
    })
    
    if (response.data.code === 200) {
      commentsList.value = response.data.data.records
      pagination.value.total = response.data.data.total
    } else {
      ElMessage.error(response.data.message || '获取留言列表失败')
    }
  } catch (error) {
    console.error('获取留言列表出错:', error)
    ElMessage.error('获取留言列表失败，请稍后再试')
  } finally {
    loading.value = false
  }
}

// 处理分页大小变化
const handleSizeChange = (val) => {
  pagination.value.size = val
  fetchCommentsList()
}

// 处理当前页变化
const handleCurrentChange = (val) => {
  pagination.value.current = val
  fetchCommentsList()
}

// 删除留言
const handleDelete = (id) => {
  ElMessageBox.confirm(
    '确定要删除这条留言吗？删除后无法恢复！',
    '删除确认',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(async () => {
    // 设置删除加载状态
    deleteLoading.value[id] = true
    try {
      const response = await instance.delete(`/review/delete/${id}`)
      
      // 根据后端响应格式处理结果
      if (response.data.code === 200) {
        // 检查data字段是否为true，表示删除成功
        if (response.data.data === true) {
          ElMessage.success('删除成功')
          // 重新获取列表
          fetchCommentsList()
        } else {
          ElMessage.error(response.data.message || '删除失败')
        }
      } else {
        ElMessage.error(response.data.message || '删除失败')
      }
    } catch (error) {
      console.error('删除留言出错:', error)
      ElMessage.error('删除失败，请稍后再试')
    } finally {
      deleteLoading.value[id] = false
    }
  }).catch(() => {
    // 用户取消删除
    ElMessage.info('已取消删除')
  })
}

// 组件挂载时获取数据
onMounted(() => {
  fetchCommentsList()
})
</script>

<style scoped>
.header {
  background-color: rgb(255, 255, 255);
  padding: 12px 20px;
  border-bottom: 1px solid #eee;
}

.main {
  background-color: rgb(255, 255, 255);
  padding: 20px 20px;
  height: calc(84vh - 100px);
  margin-top: 10px;
}

.footer {
  background-color: rgb(255, 255, 255);
  padding: 12px 20px;
  border-bottom: 1px solid #eee;
  margin-top: 15px;
}

.demo-pagination-block {
  display: flex;
  justify-content: flex-end;
}
</style>